<template>
	<view class="edit-container">
		<!-- 渐变背景 -->
		<view class="gradient-bg"></view>
		
		<!-- 自定义导航栏 -->
		<view class="custom-navbar">
			<view class="navbar-content">
				<view class="navbar-left" @click="goBack">
					<view class="back-icon-wrapper">
						<text class="back-icon">‹</text>
					</view>
					<text class="back-text">返回</text>
				</view>
				<text class="navbar-title">{{ isEdit ? '编辑备忘录' : '新建备忘录' }}</text>
				<view class="navbar-right" @click="saveMemo">
					<text class="save-text">保存</text>
				</view>
			</view>
		</view>
		
		<!-- 编辑卡片 -->
		<view class="edit-card">
			<!-- 标题输入 -->
			<view class="title-section">
				<input 
					class="title-input" 
					v-model="memoData.title"
					placeholder="请输入标题..."
					:maxlength="50"
					@input="onTitleInput"
				/>
				<text class="title-count">{{ titleLength }}/50</text>
			</view>
			
			<!-- 内容编辑 -->
			<view class="content-section">
				<textarea 
					class="content-textarea"
					v-model="memoData.content"
					placeholder="在这里记录你的想法..."
					:maxlength="2000"
					auto-height
					@input="onContentInput"
				/>
				<text class="content-count">{{ contentLength }}/2000</text>
			</view>
			
			<!-- 工具栏 -->
			<view class="toolbar">
				<view class="tool-item" @click="insertCurrentTime">
					<text class="tool-icon">🕐</text>
					<text class="tool-text">插入时间</text>
				</view>
				<view class="tool-item" @click="showCategoryPicker">
					<text class="tool-icon">📂</text>
					<text class="tool-text">分类</text>
				</view>
				<view class="tool-item" @click="toggleImportant">
					<text class="tool-icon">{{ memoData.important ? '⭐' : '☆' }}</text>
					<text class="tool-text">重要</text>
				</view>
			</view>
		</view>
		
		<!-- 分类选择器 -->
		<view class="category-modal" v-if="showCategory" @click="hideCategoryPicker">
			<view class="category-content" @click.stop>
				<view class="category-header">
					<text class="category-title">选择分类</text>
					<view class="close-btn" @click="hideCategoryPicker">
						<text class="close-icon">×</text>
					</view>
				</view>
				<view class="category-list">
					<view 
						class="category-item"
						v-for="(item, index) in categories"
						:key="index"
						:class="{ active: memoData.category === item.value }"
						@click="selectCategory(item)"
					>
						<text class="category-emoji">{{ item.emoji }}</text>
						<text class="category-name">{{ item.name }}</text>
						<text class="check-icon" v-if="memoData.category === item.value">✓</text>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 保存确认弹窗 -->
		<view class="save-modal" v-if="showSaveModal" @click="hideSaveModal">
			<view class="save-content" @click.stop>
				<view class="save-header">
					<text class="save-title">保存备忘录</text>
				</view>
				<view class="save-body">
					<text class="save-text">确定要保存这条备忘录吗？</text>
				</view>
				<view class="save-footer">
					<view class="save-btn cancel-btn" @click="hideSaveModal">
						<text class="btn-text">取消</text>
					</view>
					<view class="save-btn confirm-btn" @click="confirmSave">
						<text class="btn-text">保存</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import localStorage from '@/utils/storage.js'

export default {
	data() {
		return {
			isEdit: false,
			memoId: '',
			memoData: {
				title: '',
				content: '',
				category: 'general',
				important: false
			},
			showCategory: false,
			showSaveModal: false,
			categories: [
				{ name: '普通', value: 'general', emoji: '📝' },
				{ name: '工作', value: 'work', emoji: '💼' },
				{ name: '生活', value: 'life', emoji: '🏠' },
				{ name: '学习', value: 'study', emoji: '📚' },
				{ name: '旅行', value: 'travel', emoji: '✈️' },
				{ name: '购物', value: 'shopping', emoji: '🛒' },
				{ name: '健康', value: 'health', emoji: '💪' },
				{ name: '娱乐', value: 'entertainment', emoji: '🎮' },
				{ name: '管理', value: 'admin', emoji: '🔧' }
			]
		}
	},
	
	computed: {
		titleLength() {
			return this.memoData.title.length;
		},
		contentLength() {
			return this.memoData.content.length;
		}
	},
	
	onLoad(options) {
		if (options.id) {
			this.isEdit = true;
			this.memoId = options.id;
			this.loadMemoData();
		}
	},
	
	methods: {
		loadMemoData() {
			// 模拟加载数据
			this.memoData = {
				title: '示例标题',
				content: '这是示例内容...',
				category: 'general',
				important: false
			};
		},
		
		onTitleInput(e) {
			this.memoData.title = e.detail.value;
		},
		
		onContentInput(e) {
			this.memoData.content = e.detail.value;
		},
		
		insertCurrentTime() {
			const now = new Date();
			const timeStr = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`;
			this.memoData.content += `\n\n⏰ ${timeStr}\n`;
		},
		
		showCategoryPicker() {
			this.showCategory = true;
		},
		
		hideCategoryPicker() {
			this.showCategory = false;
		},
		
		selectCategory(item) {
			this.memoData.category = item.value;
			this.hideCategoryPicker();
		},
		
		toggleImportant() {
			this.memoData.important = !this.memoData.important;
		},
		
		goBack() {
			if (this.memoData.title.trim() || this.memoData.content.trim()) {
				uni.showModal({
					title: '提示',
					content: '有未保存的内容，确定要离开吗？',
					success: (res) => {
						if (res.confirm) {
							uni.navigateBack();
						}
					}
				});
			} else {
				uni.navigateBack();
			}
		},
		
		saveMemo() {
			if (!this.memoData.title.trim() && !this.memoData.content.trim()) {
				uni.showToast({
					title: '请输入内容',
					icon: 'none'
				});
				return;
			}
			this.showSaveModal = true;
		},
		
		hideSaveModal() {
			this.showSaveModal = false;
		},
		
		confirmSave() {
			// 验证数据
			if (!this.memoData.title.trim() && !this.memoData.content.trim()) {
				uni.showToast({
					title: '请输入内容',
					icon: 'none'
				});
				this.hideSaveModal();
				return;
			}
			
			// 使用本地存储保存备忘录
			const result = localStorage.createMemo({
				title: this.memoData.title.trim() || '无标题',
				content: this.memoData.content,
				category: this.memoData.category,
				important: this.memoData.important
			});
			
			if (result.success) {
				uni.showToast({
					title: '保存成功',
					icon: 'success'
				});
				
				setTimeout(() => {
					uni.navigateBack();
				}, 1500);
			} else {
				uni.showToast({
					title: '保存失败',
					icon: 'none'
				});
			}
			
			this.hideSaveModal();
		}
	}
}
</script>

<style lang="scss" scoped>
.edit-container {
	width: 100%;
	height: 100vh;
	position: relative;
	display: flex;
	flex-direction: column;
}

.gradient-bg {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	background: linear-gradient(135deg, #a6c0fe 0%, #f68084 100%);
	z-index: 0;
}

.custom-navbar {
	position: relative;
	z-index: 1;
	height: 88rpx;
	margin-top: var(--status-bar-height);
	background: transparent;
	padding-top: 60rpx;
}

.navbar-content {
	height: 100%;
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 0 30rpx;
}

.navbar-left {
	height: 60rpx;
	background: rgba(255, 255, 255, 0.9);
	border-radius: 30rpx;
	display: flex;
	align-items: center;
	padding: 0 20rpx;
	transition: all 0.3s ease;
	box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
}

.navbar-left:active {
	background: rgba(255, 255, 255, 0.8);
	transform: scale(0.95);
}

.back-icon-wrapper {
	width: 32rpx;
	height: 32rpx;
	border-radius: 16rpx;
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	display: flex;
	align-items: center;
	justify-content: center;
	margin-right: 12rpx;
}

.back-icon {
	font-size: 28rpx;
	color: #fff;
	font-weight: bold;
	line-height: 1;
}

.back-text {
	font-size: 26rpx;
	color: #333;
	font-weight: 500;
}

.navbar-title {
	font-size: 36rpx;
	font-weight: 600;
	color: #fff;
	text-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.navbar-right {
	height: 60rpx;
	padding: 0 24rpx;
	background: rgba(255, 255, 255, 0.9);
	border-radius: 30rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: all 0.3s ease;
	backdrop-filter: blur(10rpx);
	box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
}

.navbar-right:active {
	background: rgba(255, 255, 255, 0.8);
	transform: scale(0.95);
	box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.15);
}

.save-text {
	font-size: 28rpx;
	color: #333;
	font-weight: 600;
}

.edit-card {
	flex: 1;
	position: relative;
	z-index: 1;
	background: #ffffff;
	border-radius: 40rpx 40rpx 0 0;
	margin: 20rpx 20rpx 0;
	box-shadow: 0 -8rpx 32rpx rgba(0, 0, 0, 0.1);
	display: flex;
	flex-direction: column;
	overflow: hidden;
}

.title-section {
	padding: 60rpx 40rpx 30rpx;
	border-bottom: 1rpx solid rgba(0, 0, 0, 0.05);
	position: relative;
}

.title-input {
	width: 100%;
	font-size: 42rpx;
	font-weight: 600;
	color: #333;
	line-height: 1.3;
	border: none;
	outline: none;
	background: transparent;
}

.title-input::placeholder {
	color: #ccc;
	font-weight: 400;
}

.title-count {
	position: absolute;
	right: 40rpx;
	bottom: 30rpx;
	font-size: 24rpx;
	color: #999;
}

.content-section {
	flex: 1;
	padding: 40rpx;
	position: relative;
	display: flex;
	flex-direction: column;
}

.content-textarea {
	flex: 1;
	width: 100%;
	font-size: 32rpx;
	line-height: 1.8;
	color: #333;
	border: none;
	outline: none;
	background: transparent;
	resize: none;
	min-height: 300rpx;
}

.content-textarea::placeholder {
	color: #ccc;
}

.content-count {
	position: absolute;
	right: 40rpx;
	bottom: 40rpx;
	font-size: 24rpx;
	color: #999;
}

.toolbar {
	display: flex;
	justify-content: space-around;
	padding: 30rpx 40rpx;
	border-top: 1rpx solid rgba(0, 0, 0, 0.05);
}

.tool-item {
	display: flex;
	flex-direction: column;
	align-items: center;
	padding: 20rpx;
	border-radius: 16rpx;
	transition: all 0.3s ease;
}

.tool-item:active {
	background: rgba(0, 0, 0, 0.05);
	transform: scale(0.95);
}

.tool-icon {
	font-size: 40rpx;
	margin-bottom: 8rpx;
}

.tool-text {
	font-size: 24rpx;
	color: #666;
}

.category-modal {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	background: rgba(0, 0, 0, 0.5);
	z-index: 999;
	display: flex;
	align-items: flex-end;
	justify-content: center;
}

.category-content {
	width: 100%;
	max-height: 80%;
	background: #fff;
	border-radius: 20rpx 20rpx 0 0;
	overflow: hidden;
}

.category-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 40rpx;
	border-bottom: 1rpx solid #eee;
}

.category-title {
	font-size: 36rpx;
	font-weight: 600;
	color: #333;
}

.close-btn {
	width: 60rpx;
	height: 60rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	border-radius: 30rpx;
	background: #f8f9fa;
	transition: all 0.3s ease;
}

.close-btn:active {
	background: #e9ecef;
	transform: scale(0.95);
}

.close-icon {
	font-size: 36rpx;
	color: #666;
}

.category-list {
	max-height: 600rpx;
	overflow-y: auto;
}

.category-item {
	display: flex;
	align-items: center;
	padding: 30rpx 40rpx;
	border-bottom: 1rpx solid #f5f5f5;
	transition: all 0.3s ease;
	position: relative;
}

.category-item:active {
	background: #f8f9fa;
}

.category-item.active {
	background: rgba(102, 126, 234, 0.1);
}

.category-emoji {
	font-size: 40rpx;
	margin-right: 30rpx;
}

.category-name {
	flex: 1;
	font-size: 32rpx;
	color: #333;
}

.check-icon {
	font-size: 32rpx;
	color: #667eea;
	font-weight: bold;
}

.save-modal {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	background: rgba(0, 0, 0, 0.5);
	z-index: 999;
	display: flex;
	align-items: center;
	justify-content: center;
}

.save-content {
	width: 600rpx;
	background: #fff;
	border-radius: 20rpx;
	overflow: hidden;
	box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.3);
}

.save-header {
	padding: 40rpx 40rpx 20rpx;
	text-align: center;
}

.save-title {
	font-size: 36rpx;
	font-weight: 600;
	color: #333;
}

.save-body {
	padding: 20rpx 40rpx 40rpx;
	text-align: center;
}

.save-text {
	font-size: 28rpx;
	color: #666;
	line-height: 1.5;
}

.save-footer {
	display: flex;
	border-top: 1rpx solid #eee;
}

.save-btn {
	flex: 1;
	height: 100rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: all 0.3s ease;
}

.cancel-btn {
	border-right: 1rpx solid #eee;
}

.cancel-btn:active {
	background: #f8f9fa;
}

.confirm-btn {
	background: #667eea;
}

.confirm-btn:active {
	background: #5a6fd8;
}

.btn-text {
	font-size: 30rpx;
	font-weight: 500;
}

.cancel-btn .btn-text {
	color: #666;
}

.confirm-btn .btn-text {
	color: #fff;
}
</style> 